from flask import Flask,request
from flask import render_template
from reserch import reserch4letters


# Flask:中央登记中心
app = Flask(__name__)

# 【Flask】(https://dormousehole.readthedocs.io/en/latest/quickstart.html#id10)
# 【HTMl】(https://www.w3school.com.cn/html/index.asp)
# 【jinja2】(https://palletsprojects.com/p/jinja/)

# 1. 路由：相对路径，决定了 页面资源 的路径，如果没有登记，将返回404 not found
# 1.1 url: 相对于：http://127.0.0.1：5000的相对路径
# 1.2 methods:【GET】【POST】
# 2. 视图函数：
# 2.1 函数过程决定了你操作的方法（实现的功能），例如 if elif else for while ...
# 2.2 return：返回数据内容，text文本，HTML文件（模板渲染），JSON文件等


# 【GET】
@app.route('/',methods=['GET'])
def hello_world():
    return render_template('hello.html')

@app.route('/index',methods=['GET'])
def name():
    return render_template('index.html')

@app.route('/word',methods=['GET'])
def word():
    return render_template('word.html')

@app.route('/shopping_home',methods=['GET'])
def shopping_home():  # put application's code here
    return render_template('shopping_home.html')

# 【POST】实现用户输入，点击提交，将数据提交到 /result
@app.route('/result',methods=['POST','GET'])

def result():
    # 接收数据：request.form[HTML-参数名称]
    # 1. 名字的全称（完整的应用）：用户输入数据--->后端接收数据--->后端处理数据--->返回到HTML页面
    # 2. 元音字母查询（第二个功能）
    # 3. 自选：购物车 或者 课程表
    py_firstname = request.form['firstname']
    py_lastname = request.form['lastname']
    py_fullname = py_firstname.title() + ' ' + py_lastname.title()

    return render_template('result.html',
                           fullname=py_fullname)


@app.route('/result2', methods=['POST','GET'])
def result2():

    py_word = request.form['word']
    py_vowels = request.form['vowels']
    py_found = reserch4letters(py_vowels, py_word)
    return render_template('result2.html',
                           found = py_found)


@app.route('/shopping_result',methods=['POST'])
def shopping_result():

    py_iphone = request.form['iPhone']
    py_denglu = py_iphone

    return render_template('shopping_result.html',
                           dengji = py_denglu)

@app.route('/car_result',methods=['POST'])
def car_result():
    product_lists = [
        ("BLACKPINK迷你2辑", "150rmb"),
        ("RED VELVET应援棒", "215rmb"),
        ("SM毛封冬专", "55.8rmb"),
        ("queendom夏日迷你六辑", "105rmb"),
        ("BLACKPINK应援棒", "256rmb"),
        ("NCT应援棒", "252rmb"),
        ("ITZY正规一辑", "153rmb"),
        ("IU迷你4辑", "143rmb"),
        ("TWIVCE正规二辑", "188rmb"),
        ("TWICE应援棒", "268.5rmb"),
        ("BTS新专Butter(预售)", "290rmb"),
        ("NCT正规三辑", "150rmb")
    ]
    return render_template('car_result.html', product_lists=product_lists)

@app.route('/last', methods=['POST', 'GET'])
def last():

    name = request.form.get("name")
    tel = request.form.get('tel')
    add = request.form.get("add")
    lst = request.form.getlist('list')
    lst_str = "，".join(lst)
    return render_template('last.html',
                           name=name,
                           tel=tel,
                           add=add,
                           lst_name=lst_str)


if __name__ == '__main__':
    app.run(host='127.0.0.1',port=5000,debug=True)
